const { regeneratorRuntime } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
  }),
  dispatch => ({
    handleRoute: global.handleRoute,
    handleLicon1Tap(e) {
      console.log('handleLicon1Tap', e)
      wx.navigateBack()
    },
    async getUploadToken(data) {
      console.log('handleSubmit')
      const actionRes = await dispatch(global.globalReducer.getUploadToken(data))
      console.log(actionRes)
      if (actionRes.actionStatus !== 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          message: actionRes.apiMes
        })
      }
    },
    async handleSubmit(e) {
      console.log('handleSubmit', e.detail.value)
      try {
        // step1 upload
        const images = []
        const { filePaths } = this.data
        const { token, domain, upDomain } = this.data.uploadTokenRes.data

        const task = filePaths.map((item, index) =>
          new Promise((resolve, reject) => {
            const form = {
              url: upDomain,
              filePath: item,
              name: 'file',
              formData: {
                key: token[index].key,
                token: token[index].token
              },
            }
            wx.uploadFile({
              ...form,
              success(res) { console.log(res); resolve(res); },
              fail(res) { console.error(res); reject(res); }
            })
          })
        )

        const uploadRes = await Promise.all(task)
        uploadRes.map(i => {
          if (i.statusCode === 200) {
            images.push(JSON.parse(i.data).key)
          } else {
            throw new Error(uploadRes.errMsg)
          }
        })
        console.log('并发上传结果', uploadRes, images)

        // step2
        const { user_id, user_type } = this.data.minaLoginRes.data
        const form = {
          ...e.detail.value,
          id: user_id,
          user_type: user_type, // todo audit,store,worker
          images: images.join(',')
        }
        const actionRes = await dispatch(global.profileReducer.userAddFeedback(form))
        console.log(actionRes)
        if (actionRes.actionStatus === 'SUCCESS') {
          this.selectComponent('#toast').handleShow({
            type: 'success',
            message: actionRes.apiMes
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1000);
        } else {
          this.selectComponent('#toast').handleShow({
            message: actionRes.apiMes
          })
        }
      } catch (error) {
        console.error(error)
        this.selectComponent('#toast').handleShow({
          message: error
        })
      }
    }
  })
)({
  data: {
    uiTextMax: 200,
    uiTextLength: 0,

    uiImageMax: 4,
    uiImageCount: 4,
    filePaths: [],
    files: []
  },
  onLoad(options) {
    console.log('Modpwd onLoad')
    this.getUploadToken({ num: 4 })
  },
  handleInput(e) {
    // console.log('handleInput', e)
    const length = e.detail.value.length
    const max = this.data.uiTextMax
    this.setData({
      uiTextLength: length < max ? length : max
    })
  },
  handleChooseImage(e) {
    const that = this
    const { uiImageCount, filePaths, files } = this.data
    if (uiImageCount > 0) {
      wx.chooseImage({
        count: uiImageCount,
        sizeType: ['compressed'],
        success(res) {
          console.log(res)
          that.setData({
            filePaths: [...filePaths, ...res.tempFilePaths],
            files: [...files, ...res.tempFiles],
            uiImageCount: uiImageCount - res.tempFilePaths.length
          })
        },
        fail(res) {
          console.error(res)
        }
      })
    }
  }
}))
